﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;

namespace Zhongchouwwwww.webform
{
    public partial class Personal_Center : System.Web.UI.Page
    {
        private static int userid;
        private static string ai;

        protected void Page_Load(object sender, EventArgs e)
        {
            userid = Config.Config.ThisUser.UserId;

            if (userid>0)
            {
                if (!IsPostBack)
                {
                    DataSet(0);
                    bien(userid);
                    DataSeta(1, ai);
                }
                Bader();
                wisd();
                DataSeta(userid, ai);
                bien(userid);
                DataSet(0);
            }
            else
            {
                string strMsg = "请先登录账号"; string strUrl_Yes = "user-login.aspx"; string strUrl_No = "Index1.aspx";
                Response.Write("<Script Language='JavaScript'>if ( window.confirm('" + strMsg + "')) { window.location.href='" + strUrl_Yes +
                "' } else {window.location.href='" + strUrl_No + "' };</script>");
            }
        }

        //用户赋值
        private void Bader()
        {
            UserinfoDTO Unanem = BAL.GetDate.GetProjectBllList(userid);
            //图片设置
            this.ImageUser.ImageUrl = Unanem.imgurl;
            this.userLabel.Text = Unanem.usernikename;
            DateTime now = DateTime.Now;
            DateTime last = Unanem.registertime;
            int sj = new TimeSpan(now.Ticks - last.Ticks).Days * 1;
            if (sj==0)
            {
                this.Lajrsj.Text = "今天加入！";
            }
            else
            {
                this.Lajrsj.Text = sj.ToString() + "天";
            }
            this.Laszd.Text = Unanem.address;
            this.Labdt.Text = Unanem.userdesc;
        }

        /// <summary>
        ///获取选中的标签并隐藏和显示
        /// </summary>
        private void wisd()
        {
            string id = Request.QueryString["id"];
            if (id == null)
            {
                div1.Attributes["style"] = "display:block;height:700px";
                div2.Attributes.Add("style", "display:none");
                div3.Attributes.Add("style", "display:none");
            }
            switch (id)
            {
                case "1":
                    div1.Attributes["style"] = "display:block;";
                    div2.Attributes.Add("style", "display:none");
                    div3.Attributes.Add("style", "display:none");
                    break;
                case "2":
                    div2.Attributes["style"] = "display:block;";
                    div1.Attributes.Add("style", "display:none");
                    div3.Attributes.Add("style", "display:none");
                    break;
                case "3":
                    div3.Attributes["style"] = "display:block;";
                    div1.Attributes.Add("style", "display:none");
                    div2.Attributes.Add("style", "display:none");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 刷新喜欢项目
        /// </summary>
        private void bien(int i)
        {
            //喜欢项目
            Repeater2.DataSource = BAL.GetDate.GetProjectAllList(ai, i);
            Repeater2.DataBind();
        }
        /// <summary>
        /// 刷新发起项目
        /// </summary>
        /// <param name="i"></param>
        private void DataSet(int i)
        {
            Redata.DataSource = BAL.GetDate.GetProjectAllList(i, userid);
            Redata.DataBind();
        }

        /// <summary>
        /// 刷新支持的项目
        /// </summary>
        /// <param name="i"></param>
        /// <param name="a"></param>
        private void DataSeta(int i, string a)
        {
            List<ProjectinfoDTO> na = new List<ProjectinfoDTO>();
            na = BAL.GetDate.GetProjectAllList(ai, i, ai);
            SortedList<int, ProjectinfoDTO> TempDate = new SortedList<int, ProjectinfoDTO>();
            foreach (var item in na)
            {
                try
                {
                    TempDate.Add(item.projectid, item);
                }
                catch (Exception)
                {
                    continue;
                }
            }
            List<ProjectinfoDTO> LastDate = new List<ProjectinfoDTO>();
            foreach (var item in TempDate)
            {
                LastDate.Add(item.Value);
            }

            Repeater1.DataSource = LastDate;
            Repeater1.DataBind();
        }



        protected void butsy_Click(object sender, EventArgs e)
        {
            DataSet(1);
        }

        protected void butsbz_Click(object sender, EventArgs e)
        {
            DataSet(2);
        }

        protected void butycg_Click(object sender, EventArgs e)
        {
            DataSet(3);
        }

        protected void butysb_Click(object sender, EventArgs e)
        {
            DataSet(4);
        }

        protected void faqxm_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-Center.aspx?id=2");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-Center.aspx?id=1");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-Center.aspx?id=3");
        }

        //图片上传
        protected void ImageUser_Click(object sender, ImageClickEventArgs e)
        {
            //获取上传的文件名
            string fileName = FileUpload1.FileName;
            //获取上传文件的文件后缀名
            String fileFix = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
            Random ran = new Random();
            //随机数
            int po = ran.Next(00000, 99999);
            //判断是否是图片格式
            if (fileFix != "png" && fileFix != "jpeg" && fileFix != "pic" && fileFix != "jpg")
            {
                string strMsg = "请上传图片"; string strUrl_Yes = "Personal-Center.aspx"; string strUrl_No = "Personal-Center.aspx";
                Response.Write("<Script Language='JavaScript'>if ( window.confirm('" + strMsg + "')) { window.location.href='" + strUrl_Yes +
                "' } else {window.location.href='" + strUrl_No + "' };</script>");

            }
            else
            {
                try
                {
                    FileUpload1.SaveAs(Server.MapPath("..") + "//Images//" + (po) + fileName);
                    bool retlr = BAL.GetDate.update(fileName, userid, po);
                    if (retlr)
                    {
                        ImageUser.ImageUrl = "../images/" + (po) + fileName;
                    }
                }
                catch (Exception)
                {
                    Response.Write("<script>alert(' 上传失败')< script >");
                    throw;
                }
            }
        }

        //获取删除获取要预览的项目id+
        protected void Redata_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //删除
            if (e.CommandName.ToString() == "Remove")
            {
                string iddd = null;
                string zt = e.CommandArgument.ToString().Substring(0, 2);
                if (zt == "众筹")
                {
                }
                else if (zt == "草稿")
                {
                    iddd = e.CommandArgument.ToString().Substring(2, e.CommandArgument.ToString().Length - 2);
                }
                else if(zt=="待审")
                {
                    iddd = e.CommandArgument.ToString().Substring(3, e.CommandArgument.ToString().Length - 3);
                }
                else
                {
                    iddd = null;
                }
                if (iddd != null)
                {
                    BAL.GetDate.delet(iddd);
                }
            }
            else if (e.CommandName.ToString() == "Lsngxx")//编辑或预览
            {
                string projectid = null;
                string zt = e.CommandArgument.ToString().Substring(0, 2);
                if (zt == "草稿")
                {
                    string idaa = e.CommandArgument.ToString().Substring(2, e.CommandArgument.ToString().Length - 2);
                    int cgid = BAL.GetDate.db.Database.SqlQuery<int>($"select prostate from Projectinfo where projectid='{idaa}'").First();
                    //根据此项目字段跳转编辑页面
                    switch (cgid)
                    {
                        case 1:
                            Response.Redirect($"project-returns-set.aspx?id=1&num={idaa}");
                            break;
                        case 2:
                            Response.Redirect($"project-initiator-mesg.aspx?id=1&num={idaa}");
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    string zt1 = e.CommandArgument.ToString().Substring(0, 3);
                    if (zt1 != "已失败")
                    {
                        projectid = e.CommandArgument.ToString().Substring(3, e.CommandArgument.ToString().Length - 3);
                    }
                    if (zt1 == "待审核")
                    {
                        string strUrl_Yes = "Personal-Center.aspx"; string strUrl_No = "Personal-Center.aspx";
                        Response.Write("<Script Language='JavaScript'>if ( window.confirm('" + "待审核中" + "')) { window.location.href='" + strUrl_Yes +
                        "' } else {window.location.href='" + strUrl_No + "' };</script>");
                    }
                    else
                    {
                        if (projectid != null)
                        {
                            Response.Redirect($"Project.aspx?id={projectid}");
                        }
                    }
                    
                }

            }

        }

        //注销
        protected void Unnamed_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Config.Config.ThisUser.UserId = 0;
            Config.Config.ThisUser.UserName = "";
            Response.Redirect("Index1.aspx");
        }
    }

}
    